]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/Tests/Unit Tests/mDNSCoreReceiveTest.m
mDNSResponder-1096.60.2.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / Tests / Unit Tests / mDNSCoreReceiveTest.m
1 /*
2 * Copyright (c) 2017-2018 Apple Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "unittest_common.h"
18 #import <XCTest/XCTest.h>
19
20 // This DNS message was gleaned from a uDNS query request packet that was captured with Wireshark.
21 uint8_t udns_query_request_message[28] = { // contains 1 question for www.f5.com
22 0x31, 0xca, // transaction id
23 0x01, 0x00, // flags
24 0x00, 0x01, // 1 question
25 0x00, 0x00, // no anwsers
26 0x00, 0x00, // no authoritative answers
27 0x00, 0x00, // no additionals
28 0x03, 0x77, 0x77, 0x77, 0x02, 0x66, 0x35, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01
29 };
30
31 // This DNS message was gleaned from a uDNS query request packet that was captured with Wireshark.
32 // Then the header id (more specifically, the msg->h.id) was deliberately cleared to force code
33 // path to traverse regression case, <rdar://problem/28556513>.
34 uint8_t udns_query_request_message_with_invalid_id[28] = { // contains 1 question for www.f5.com, msg->h.id = 0
35 0x00, 0x00, // transaction id
36 0x01, 0x00, // flags
37 0x00, 0x01, // 1 question
38 0x00, 0x00, // no anwsers
39 0x00, 0x00, // no authoritative answers
40 0x00, 0x00, // no additionals
41 0x03, 0x77, 0x77, 0x77, 0x02, 0x66, 0x35, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01
42 };
43
44 uint8_t arp_request_packet[28] = { // contains 1 question for www.f5.com, msg->h.id = 0
45 0x00, 0x01, // hardware type: enet
46 0x08, 0x00, // protocol type: IP
47 0x06, // hardware size
48 0x04, // Protcol size
49 0x00, 0x01, // opcode request
50 0x24, 0x01, 0xc7, 0x24, 0x35, 0x00, // Sender mac addr
51 0x11, 0xe2, 0x14, 0x01, // Sender ip addr
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // target mac addr
53 0x11, 0xe2, 0x17, 0xbe // target ip addr
54 };
55
56 mDNSlocal void InitmDNSStorage(mDNS *const m)
57 {
58 memset(m, 0, sizeof(mDNS));
59 }
60
61 @interface mDNSCoreReceiveTest : XCTestCase
62 {
63 }
64 @end
65
66 @implementation mDNSCoreReceiveTest
67
68 - (void)setUp
69 {
70 mDNSPlatformTimeInit();
71 mDNS_LoggingEnabled = 0;
72 mDNS_PacketLoggingEnabled = 0;
73 }
74
75 - (void)tearDown {
76 }
77
78 - (void)testValidQueryReq
79 {
80 mDNS *const m = &mDNSStorage;
81 mDNSAddr srcaddr, dstaddr;
82 mDNSIPPort srcport, dstport;
83 DNSMessage * msg;
84 const mDNSu8 * end;
85
86 // Init unit test environment and verify no error occurred.
87 mStatus result = init_mdns_environment(mDNStrue);
88 XCTAssertEqual(result, mStatus_NoError);
89
90 // Used random values for srcaddr and srcport
91 srcaddr.type = mDNSAddrType_IPv4;
92 srcaddr.ip.v4.b[0] = 192;
93 srcaddr.ip.v4.b[1] = 168;
94 srcaddr.ip.v4.b[2] = 1;
95 srcaddr.ip.v4.b[3] = 10;
96 srcport.NotAnInteger = swap16((mDNSu16)53);
97
98 // Used random values for dstaddr and dstport
99 dstaddr.type = mDNSAddrType_IPv4;
100 dstaddr.ip.v4.b[0] = 192;
101 dstaddr.ip.v4.b[1] = 168;
102 dstaddr.ip.v4.b[2] = 1;
103 dstaddr.ip.v4.b[3] = 20;
104 dstport.NotAnInteger = swap16((mDNSu16)49339);
105
106 // Set message to a DNS message (copied from a WireShark packet)
107 msg = (DNSMessage *)udns_query_request_message;
108 end = udns_query_request_message + sizeof(udns_query_request_message);
109
110 // Execute mDNSCoreReceive using a valid DNS message
111 mDNSCoreReceive(m, msg, end, &srcaddr, srcport, &dstaddr, dstport, if_nametoindex("en0"));
112
113 // Verify that mDNSCoreReceiveQuery traversed the normal code path
114 XCTAssertEqual(m->mDNSStats.NormalQueries, 1);
115 }
116
117 - (void)testNullDstQueryReqTest
118 {
119 mDNS *const m = &mDNSStorage;
120 mDNSAddr srcaddr;
121 mDNSIPPort srcport, dstport;
122 DNSMessage * msg;
123 const mDNSu8 * end;
124
125 // This test case does not require setup of interfaces, the record's cache, or pending questions
126 // so m is initialized to all zeros.
127 InitmDNSStorage(m);
128
129 // Used random values for srcaddr and srcport
130 srcaddr.type = mDNSAddrType_IPv4;
131 srcaddr.ip.v4.b[0] = 192;
132 srcaddr.ip.v4.b[1] = 168;
133 srcaddr.ip.v4.b[2] = 1;
134 srcaddr.ip.v4.b[3] = 10;
135 srcport.NotAnInteger = swap16((mDNSu16)53);
136
137 // Used random value for dstport
138 dstport.NotAnInteger = swap16((mDNSu16)49339);
139
140 // Set message to a DNS message (copied from a WireShark packet)
141 msg = (DNSMessage *)udns_query_request_message_with_invalid_id;
142 end = udns_query_request_message_with_invalid_id + sizeof(udns_query_request_message_with_invalid_id);
143
144 // Execute mDNSCoreReceive to regress <rdar://problem/28556513>
145 mDNSCoreReceive(m, msg, end, &srcaddr, srcport, NULL, dstport, if_nametoindex("en0"));
146
147 // Verify that mDNSCoreReceiveQuery was NOT traversed through the normal code path
148 XCTAssertEqual(m->mDNSStats.NormalQueries, 0);
149
150 // Verify code path that previously crashed, in <rdar://problem/28556513>, now traverses successfully
151 // by checking a counter that was incremented on code path that crashed.
152 XCTAssertEqual(m->MPktNum, 1);
153 }
154
155 #if 0
156 - (void)testPerformanceExample {
157 // This is an example of a performance test case.
158 [self measureBlock:^{
159 // Put the code you want to measure the time of here.
160 }];
161 }
162 #endif
163
164 @end